perm filename SEC2.TEX[105,CSD] blob
sn#537111 filedate 1980-09-18 generic text, type C, neo UTF8
COMMENT ⊗ VALID 00009 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 \specialbegin{About Definitions}\sendindex{Definitions}
C00013 00003 \concept
C00022 00004 \sneakhead{Spacing} \sendindex{spacing}
C00025 00005 \sneakhead{Printing Format}\sendindex{Printing Format}
C00029 00006 \sneakhead{String Constants} \sendindex{string constants}
C00033 00007 \specialbegin{Problem Decomposition} \sendindex{Program Decomposition}
C00050 00008 \specialbegin{Comments in Programs}
C00053 00009 \specialbegin{Professionalism}
C00056 ENDMK
C⊗;
\specialbegin{About Definitions}\sendindex{Definitions}
In describing Pascal, we shall distinguish between {\sl syntax}, \sendindex{syntax}
{\sl semantics}, \sendindex{semantics}
and {\sl pragmatics}. \sendindex{pragmatics}
{\sl Syntax} is the
grammar of the language; it specifies which programs are meaningful, and how a
program is subdivided into meaningful parts. {\sl Semantics} is concerned with
defining meaning; it specifies what each program does when executed, usually
either by saying how the program carries out its actions, or by saying how to
construct a simpler equivalent program. {\sl Pragmatics} is concerned with
practicality; it states what execution of a program will cost, usually as
measured in units of time or money, whether execution of a program will require
more of some resource than is avalable on a particular computer, and what the
value of the results will be (particularly how precisely they are calculated).
For the purposes of syntactic definition, we will eventually give precise
definitions to a number of kinds of things we can write as parts of programs.
Among these are the following:
\yyskip
\bitem {\sl Constants}: \sendindex{constants} designate a particular number,
word, or object with which computation is done.
\displayline{Examples: {\tt 2, 3.14159 'MISSISSIPPI'}}
\bitem {\sl Identifiers}: \sendindex{identifiers} used as names for anything
to which a programmer may give a name: constants, variables, functions,
tables, part of programs.
\displayline{Examples: {\tt A, X, SINE, LOGTABLE, A3PQ14, TIMEOFDAY}}
\bitem {\sl Variables}: \sendindex{variables} identifiers which have appeared
in a {\tt VAR} declaration.\sendindex{VAR declaration}
\displayline{Examples: {\tt VAR A,X : INTEGER; $\ldots$ PRINT(A)}}
\bitem {\sl Expressions}: \sendindex{expressions} designate a way of
computing a value for subsequent
use in the program. Since an expression may contain variables which
have different vaues at different times, the computation which an
expression describes must usually be done again each separate time
the value of the expression is needed.
\displayline{Examples: {\tt X, 13.54, COS(3.141592654/6), 2*A, B-C}}
\bitem {\sl Commands}: \sendindex{commands} designate an action or series of
actions to be carried
out by the computer, such as calculations, printing, deciding
between two courses of action, etc.
\displayline{Example: {\tt FOR I := 2 TO 5 DO WRITE (I)}}
\yyskip
In describing the grammatical structure of Pascal, we will use the
script letters \sendindex{script letters} $\Kscr$, $\Iscr$, $\Vscr$,
$\Escr$, $\Cscr$, possibly with subscripts, diacritical marks, etc., as
symbols for arbitrary constants, identifiers, variables, expressions, and commands.
For example, the form of an iterative command can be described as
\wuncode{FOR $\Vscr$ := $\Eone$ TO $\Etwo$ DO $\Cscr$}
where $\Vscr$ is a variable, $\Eone$ and $\Etwo$ are expressions
having integer values, and $\Cscr$ is any command.
We will later introduce other grammatical categories. For each, we will
have a script capital letter to stand for an arbitrary member of that category.
Often it will be necessary to define the meanings of commands, etc., in
which one or more expressions appear, and for which the process of execution
requires evaluation of the expressions. In such circumstances, we will use
the lower case letter $\alpha$ to designate the value of the
expression $\Escr$, i.e.,
the number, word, etc., which results from evaluating $\Escr$. If $\Escr$ is the
expression {\tt (3+5)*12}, the value $\alpha$ is 96. (More precisely, $\alpha$ is the
number which we designate in decimal notation by 96 ; in Roman numerals,
$\alpha$ would be designated by XCVI or, in binary notation, by 1100000, but
would still be the same value.) Similarly, we use $\alpha ↓{1}$ and $\alpha$'
to refer to
the values of $\Escr ↓{1}$ and $\Escr$' respectively. To say concisely that if an
expression consists of two smaller expressions connected by an asterisk, the
value of the expression is the product of the values of the two smaller
expressions, we could say
``If $\Escr$ is $\Escr ↓{1}$*$\Escr ↓{2}$, then $\alpha$ = $\alpha ↓{1}\times$
$\alpha ↓{2}$.''
In general, we use script capital letters in stating syntactic rules and
relations, and lower case letters in stating semantic relations; we will find
ourselves using both when we want to say ``To express such-and-such meanings,
use such-and-such forms.''
Often practical considerations complicate the definition of a programming
language. For example, computation is done only with numbers up to a certain
size; computations which are intended to give results larger than the allowed
size give erroneous results or stop the program. For example, the rule that
``If $\Escr$ is $\Escr ↓{1}$*$\Escr ↓{2}$,
then $\alpha$ = $\alpha ↓{1}\times \alpha ↓{2}$''
is only valid if $\alpha ↓{1}\times \alpha ↓{2}$ is within the allowed
range of sizes for numbers.
Such pragmatic considerations complicate definitions, and can be forgotten
most of the time in elementary programming.
We may now give more precise and complete definitions of the aspects of
Pascal which have already been introduced.
There are two fundamental printing commands:
\wuncode{WRITE($\Oscr$)}\sendindex{WRITE}
where $\Oscr$ is either an expression whose value is a number, or a message in
single quotation marks, prints the value of $\Oscr$ in the output file.
\wuncode{WRITELN}
begins a new line on the output file; it is equivalent, roughly, to
typing a carriage return.
Other printing commands are abbreviated combinations of these:
\samecode{
WRITE($\Oscr ↓{1}$,$\Oscr ↓{2}$,$\ldotss$,$\Oscr ↓{n}$)⊗WRITE($\Oscr ↓{1}$);\cr
⊗WRITE($\Oscr ↓{2}$);\cr
⊗\quad .\cr
⊗\quad .\cr
⊗\quad .\cr
⊗WRITE($\Oscr ↓{n}$)\cr
}
You must pay attention to the number of characters which can be displayed
on a single line. Your terminal screen will display 80 characters per
line. The line printer will display 132 characters per line.
If you print more than one line's worth of information between
{\tt WRITELN}s, some of your output will be lost or garbled.
%Be careful not to print more than one line's worth of information between
%{\tt WRITELN}s, or some of your output will be lost or garbled.
\samecode{
WRITELN($\Oscr ↓{1}$,$\Oscr ↓{2}$,$\ldotss$,$\Oscr ↓{n}$)⊗WRITE($\Oscr ↓{1}$);\cr
⊗WRITE($\Oscr ↓{2}$);\cr
⊗\quad .\cr
⊗\quad .\cr
⊗\quad .\cr
⊗WRITE($\Oscr ↓{n}$);\cr
⊗WRITELN\cr
}
\concept
A numerical expression, \sendindex{expression} if it is not a constant
or a variable, has one of
the forms given below in the left column, where the corresponding value is shown
in the right column.
\sendindex{DIV}
\twocolalign{
\under{\bf Form}⊗\under{\bf Value}\cr
$\Escr ↓1+\Escr ↓2$⊗$\alpha ↓1+ \alpha ↓2$\cr
$\Escr ↓{1}-\Escr ↓{2}$⊗$\alpha ↓{1}- \alpha ↓{2}$\cr
$+\Escr$⊗$\alpha$\cr
$-\Escr$⊗$- \alpha$\cr
$\Escr ↓{1}*\Escr ↓{2}$⊗$\alpha ↓{1}\times \alpha ↓{2}$\cr
$\Escr ↓1/\Escr ↓{2}$⊗$\alpha ↓{1}/ \alpha ↓{2}$\quad(undefined if $\alpha ↓{2} = 0$)\cr
$\Escr ↓{1}$ {\tt DIV} $\Escr ↓{2}$⊗The integer part of $\alpha ↓{1}/ \alpha ↓{2}$, which must both be integers.\cr
$(\Escr )$⊗$\alpha$\cr
{\tt ABS}$(\Escr )$⊗$|\alpha |$, the absolute value of $\alpha$\cr
{\tt SQR}($\Escr$)⊗$\alpha ↑2$\cr
{\tt SIN}($\Escr$)⊗$sin(\alpha)$, $\alpha$ in radians\cr
{\tt COS}($\Escr$)⊗$cos(\alpha)$, $\alpha$ in radians\cr
{\tt LN}($\Escr$)⊗$log ↓e(\alpha), \alpha > 0$\cr
{\tt EXP}($\Escr$)⊗$e↑\alpha$\cr
{\tt SQRT}($\Escr$)⊗$\sqrt{\alpha}, \alpha >= 0$\cr
{\tt ARCTAN}($\Escr$)⊗$tan↑{-1}(\alpha)$, in range $(-π/2, π/2)$\cr
}%end of twocolalign
\sendindex{ABS}
\sendindex{SQR}
\sendindex{SIN}
\sendindex{COS}
\sendindex{LN}
\sendindex{EXP}
\sendindex{SQRT}
\sendindex{ARCTAN}
A string expression \sendindex{string expression} is any sequence of
symbols enclosed in (single) quotation
marks. (Still other forms of expression will be described later.)
In the table above, the right-hand column lists a number of operations
such as multiplication and finding square roots, which occur frequently in
computation and are therefore made available as {\sl primitives} \sendindex{primitives}
(operations which
do not have to be programmed from simpler operations and commands) in Pascal.
Some of them, such as the arithmetic operations, are primitives of the computer
language into which Pascal programs are translated. Others, such as the square
root operation, are not computer language primitives; the translator
automatically includes a short computer language program to perform them, if
they are used in a Pascal program. The left-hand column shows the way that these
operations are written in Pascal. Sometimes, as with addition and the sin
function, the Pascal notation for the function is the familiar way of writing
the operation, but in other cases, such as multiplication and square root, the
limitations of computing equipment (especially the keyboard) force the use of
an unfamiliar notation. We will tend to use lower case letters ($log↓e$) when
talking about the operation itself (semantics), and capital letters (LN)
when talking about the way it is expressed in Pascal (syntax).
\sendindex{program}
\sendindex{heading}
\concept
A program consists of a heading, a block, and a period.
\sendindex{block}
\sendindex{period}
The heading is of the form
\wuncode{PROGRAM $\Nscr$ ($\Nscr↓{1}, \Nscr↓{2}, \ldotss, \Nscr↓{n}$);}
where $\Nscr$ is the name of the program, and $\Nscr↓{1}$, etc, are the names of the
files the program makes use of. Even if not mentioned in the heading, files
named INPUT and OUTPUT may be used by the program.
If no other files than OUTPUT and INPUT are used, the heading may be simplified to
\wuncode{PROGRAM name;}
followed, as before, by a block and a period.
\sendindex{INPUT}
\sendindex{OUTPUT}
\concept
A block \sendindex{block} ($\Bscr$) is of the form
\wuncode{$\Dscr ↓{1}$; $\Dscr ↓{2}$; $\ldots$; $\Dscr ↓{m}$; BEGIN $\Cscr ↓{1}$; $\Cscr ↓{2}$; $\ldots$; $\Cscr ↓{n}$ END}
where $\Dscr ↓{1}$, etc, are declarations and $\Cscr ↓{1}$, etc, are commands (statements).
\sendindex{BEGIN}
\sendindex{END}
To execute a block, one executes the commands in the order written. Variables
declared in a block may only be referred to in that block.
\concept
An identifier \sendindex{identifier} ($\Iscr$) is a sequence of letters
and/or digits, starting with
a letter. Identifiers, thus far, have been introduced only as names of
variables.
The identifiers are
\startcode
A,B,C,$\ldotss$,X,Y,Z,AA,AB,$\ldotss$,AZ,A0,A1,$\ldotss$,A9,
BA,$\ldotss$,B9,$\ldotss$,ZA,$\ldotss$,Z9,AAA,$\ldotss$,
EXAMPLE,$\ldotss$,EX12A83,$\ldotss$,YETLONGEREXAMPLE,$\ldots$
VERYMUCHLONGEREXAMPLEWHICHYOUWOULDQUICKLYTIREOFWRITING,$\ldots$
\endcode
Words which have a fixed meaning in Pascal, such as {\tt BEGIN, FOR,}
and {\tt END,} may not be used as identifiers. The {\sl Pascal User Manual},
page 109,
contains a list of these
{\sl reserved words}. \sendindex{reserved words}
(The list may also be found in {\sl Pascal at LOTS}, page 54.)
\concept
A {\sl declaration} \sendindex{declaration} ($\Dscr$) is of the form
\wuncode{VAR $\Iscr ↓{1}$, $\Iscr ↓{2}$, $\ldotss$, $\Iscr ↓{n}$ : INTEGER}
or
\wuncode{VAR $\Iscr ↓{1}$, $\Iscr ↓{2}$, $\ldotss$, $\Iscr ↓{n}$ : REAL}
\sendindex{VAR}
\sendindex{INTEGER}
\sendindex{REAL}
\concept
A {\sl variable} \sendindex{variable} ($\Vscr$) is an identifier which
has appeared in such a
declaration. We will
also encounter other kinds of variables and declarations.
\sneakhead{Spacing} \sendindex{spacing}
In the preparation of a program, one may freely use blank spaces, or even
entire blank lines. Like the use of paragraphs, margins and punctuation in
English writing, this is an art of some importance in maintaining
intelligibility. Some ways to use spacing effectively will be discussed
later. For the present, we need only say that spaces may not be inserted into a
number, word, or variable, and that if a number, word, or variable is
immediately followed by another of these, there must be at least one space
between them. Thus one must not write
\wuncode{FORI:=ATO100DOWRITE(I,1/I)}
but rather
\wuncode{FOR I:=A TO 100 DO WRITE(I,1/I)}
where only the necessary spaces have been inserted. The end of a line counts
as a space. (This fact limits identifiers to about 80 characters.) In
general, if a program is legible, the use of spaces is probably correct.
Commas are not permitted as punctuation within a number; one must not write
a number as
\wuncode{1,234,567.890}
but rather
\wuncode{1234567.890}
\exercise
What does the program
\wuncode{WRITE(1,234,567.890)}
do?
\endexercise
\sneakhead{Printing Format}\sendindex{Printing Format}
Integer values are the values of expressions written without using decimal
points, using only integer variables, and using only the functions and
operators +, -, *, DIV, ABS, and a few others we will meet later. Integer
values\sendindex{integer values} are printed as several blanks, a minus sign if needed, and several
decimal digits, making up twelve characters in all, so up to eleven integers can
appear across a printed page, up to six across the terminal screen.
\sendnotes{RWF==>RWF Redo}
``Real'' values are the values of other numerical expressions, e.g. those
containing a decimal point (13.53, or even 13.00), a `/',
a SIN, COS, SQRT, etc., or a variable declared to be REAL.
%Real values are printed as several blanks, a minus sign if needed, and several decimal
%digits containing a decimal point, making up sixteen characters in all, so
%up to eight real values can appear across a printed page, and up to five
%across the terminal screen.
%Very large or very small real numbers are printed differently to fit into
%sixteen characters.
Real values are printed in a form of scientific notation, as exemplified below:
\sendnotes{RWF==>Correct these.}
\twocolalign{
{\tt Value}⊗{\tt Printed Form}\cr
$1$⊗{\tt ≡1.000000000≡≡≡≡}\cr
$22/7$⊗{\tt ≡3.142857143≡≡≡≡}\cr
$-1234.56789 (ie, 1.23456789 \times 10↑3)$⊗{\tt -1.234567890E+03}\cr
$0.00123456789 (ie, 1.23456789 \times 10↑{-3})$⊗{\tt ≡1.234567890E-03}\cr
}%end of twocolalign
%\twocolalign{
%Value⊗Printed Form\cr
%$1$⊗$≡1.000000000≡≡≡≡$\cr
%$22/7$⊗$\3.142857143≡≡≡≡$\cr
%$-1234.56789 (ie, 1.23456789 x 10↑3$⊗$-1.234567890\ E\ 03$\cr
%$0.00123456789 (ie,1.23456789 x 10↑{-3}$⊗≡$1.234567890 E-03$\cr
%}%end of twocolalign
\noindent
The format consists of a blank or minus sign, a ten digit number with decimal
point after the first digit, ``{\tt E}'', a two digit power of ten. If the
number lies baetween 1 and 9.999999999, the power of ten is omitted. In
all, sixteen characters are needed for a real value, so up to eight real
values can appear across a printed page, and up to five across the terminal
screen.
\sneakhead{String Constants} \sendindex{string constants}
In addition to numerical values, we have seen that one may print values
which are sequences of symbols, or {\sl characters}. \sendindex{characters}
Such values are called {\sl strings}\/; \sendindex{strings}
words, and decimal representations of numbers, are particular kinds
of strings. The constant representing a particular string is written by
enclosing the string between double quote marks. Since the arithmetical
operators and functions with which we are as yet acquainted are obviously not
applicable to strings, the only use which we can as yet make of strings is to
print them. For example, the program:
\startcode
WRITELN(1.5,'1.5',2*3,'2*3');
WRITELN('ABCDEF+-*/1234')
\endcode
\noindent
causes the printing of the following lines:
\topoutput
1.500000 1.5 6 2*3
ABCDEF+-*/1234
\botoutput
When writing a program using string constants which contain blanks, it
is customary to use the symbol ``{\tt ≡}'' to stand for a blank,
and then to type
the ``{\tt ≡}'' as a blank. For example, to make a program print
\topoutput
*
* *
* *
\botoutput
\noindent
one would write the program
\startcode
WRITELN('≡≡*');
WRITELN('≡*≡*');
WRITELN('*≡≡≡*')
\endcode
\noindent
and then type it at the terminal keyboard
\startcode
WRITELN(' *');
WRITELN(' * *');
WRITELN('* *')
\endcode
\example
To print the square like that shown below, in a larger size, say eight
inches on a side,
\topoutput
**********
* *
* *
* *
* *
**********
\botoutput
\noindent
we could write the program
\startcode
FOR I:= 1 STEP 1 UNTIL 80 DO
WRITE('*');
WRITELN;
FOR I:= 1 STEP 1 UNTIL 46 DO
BEGIN
WRITE('*');
FOR J:=1 STEP 1 UNTIL 78 DO
WRITE('≡');
WRITELN('*');
END;
FOR I:=1 STEP 1 UNTIL 80 DO
WRITE('*');
WRITELN;
\endcode
(The last line keeps any later printing from going on the bottom line of the
square.)
\specialbegin{Problem Decomposition} \sendindex{Program Decomposition}
Let us consider the problem of printing a small table of natural
logarithms of the numbers from 10 to 99. Envision the general appearance of
the table as
\topoutput
TABLE OF LOGARITHMS
0 1 2 3 4
10 log 10 log 11 $\ldots$ log 14
15 log 15 log 16 $\ldots$ log 19
20 log 20
25 log 25
30 log 30
.
.
.
95 log 95 log 99
\botoutput
If we had available a fantastically rich and expressive programming
language, we might write in it simply
\wuncode{WRITE A TABLE OF LOGARITHMS WITH HEADINGS $\ldotss$, etc.}
As we do not, we must repeatedly decompose the task into sequences, and
iterations, of simpler tasks, until we have reduced it to tasks each of which
can be carried out by a single command in Pascal.
We begin by observing that the first two lines of the table, being a title
and heading, are completely different in structure from the rest. Because the
printing mechanism prints one line after another, we may decompose our task
into the following ``program'':
\startcode
PROGRAM LOGTABLE;
(declarations)
BEGIN
Print the title;
Print the heading;
Print the body of the table
END.
\endcode
These are not Pascal commands, nor do they completely define the particular
table we want, so that the program is not yet executable by a computer.
However, if we can find commands to do the three parts of the task, we can
put them together in such a block to do the entire task. We can express
\wuncode{Print the title}
in Pascal as
\startcode
FOR I := 1 TO 40 DO WRITE(' ');
WRITE('TABLE OF LOGARITHMS');
WRITELN;
WRITELN;
\endcode
The heading consists of two qualitatively distinct parts:
a blank region above the 10, and then a series of numbers, so we decompose
\wuncode{Print the heading.}
into
\startcode
WRITE(' ');
FOR I:=0 TO 4 DO WRITE(I,' ');
WRITELN;
WRITELN;
\endcode
Looking at the body of the table, we see that it consists of eighteen
lines, which must be printed in sequence, and whose differences from each other
can be characterized by their numerical dependence on a variable. This is a
natural situation for decomposition, into an iteration of one command which can
print any line of the table body. Thus
\wuncode{Print the body of the table}
reduces to
\startcode
Iterate the following, with a = 2,3,4$\ldotss$,19;
Write a line of the form
5*a,log(5*a),log(5*a+1),$\ldotss$,log(5*a+4)
\endcode
Now the program has become much less abstract; it has been decomposed into the
following structure:
\startcode
PROGRAM LOGTABLE;
(declarations)
BEGIN
FOR I := 1 TO 40 DO WRITE(' ');
WRITE('TABLE OF LOGARITHMS');
WRITELN;
WRITELN;
\allowbreak
WRITE(' ');
FOR I:=0 TO 4 DO WRITE(I,' ');
WRITELN;
WRITELN;
\allowbreak
Iterate the following, with A = 2,3,$\ldotss$,19:
Write a line of the form
5*a,log(5*a),log(5*a+1),$\ldotss$,log(5*a+4)
END.
\endcode
We may decompose
\startcode
Write a line of the form
5*a,log(5*a),log(5*a+1),$\ldotss$,log(5*a+4)
\endcode
\noindent
into
\startcode
WRITE(5*A);
Write log(5*A) log(5*A+1) $\ldots$ log(5*A+4);
WRITELN;
\endcode
\noindent
and further decompose the second line above into
\startcode
FOR C:=0 TO 4 DO
WRITE(LN(5*A+C));
\endcode
\noindent
or into
\startcode
FOR C:=5*A TO 5*A+4 DO
WRITE(LN(C));
\endcode
\vfill\eject\sendnotes{FINALCHECK: This eject put in to eliminate a widow.}
Combining these pieces, we get this complete program:
\startcode
PROGRAM LOGTABLE;
VAR I,A,C : INTEGER;
BEGIN
FOR I := 1 TO 40 DO WRITE(' ');
WRITE('TABLE OF LOGARITHMS');
WRITELN;
WRITELN;
\allowbreak
WRITE(' ');
FOR I:=0 TO 4 DO WRITE(I,' ');
WRITELN;
WRITELN;
\allowbreak
FOR A:=2 TO 19 DO
BEGIN
WRITE(5*A);
FOR C:=5*A TO 5*A+4 DO
WRITE(LN(C));
WRITELN
END
END.
\endcode
%\noindent
%which when executed, prints the following (modified slightly to fit on this
%page):
\vfill\eject\sendnotes{FINALCHECK: Check unconditional eject.}
This program prints the following table (modified slightly to fit on the
page.)
\topoutput
TABLE OF LOGARITHMS
0 1 2 3 4
10 2.302585 2.397895 2.484907 2.564949 2.639057
15 2.708050 2.772589 2.833213 2.890372 2.944439
20 2.995732 3.044522 3.091042 3.135494 3.178054
25 3.218876 3.258097 3.295837 3.332205 3.367296
30 3.401197 3.433987 3.465736 3.496508 3.526361
35 3.555348 3.583519 3.610918 3.637586 3.663562
40 3.688879 3.713572 3.737670 3.761200 3.784190
45 3.806663 3.828641 3.850148 3.871201 3.891820
50 3.912023 3.931826 3.951244 3.970292 3.988984
55 4.007333 4.025352 4.043051 4.060443 4.077537
60 4.094345 4.110874 4.127134 4.143135 4.158883
65 4.174387 4.189655 4.204693 4.219508 4.234107
70 4.248495 4.262680 4.276666 4.290460 4.304065
75 4.317488 4.330733 4.343805 4.356709 4.369448
80 4.382027 4.394449 4.406719 4.418841 4.430817
85 4.442651 4.454347 4.465908 4.477337 4.488636
90 4.499810 4.510860 4.521789 4.532599 4.543295
95 4.553877 4.564348 4.574711 4.584968 4.595120
\botoutput
The general principles of decomposition\sendindex{decomposition} we have used,
so far, are these:
\bitem If a task can be carried out by a single Pascal command, use that command.
\bitem If a task can be carried out by a short sequence of dissimilar processes,
decompose it into a sequence of commands to perform those processes. If
that sequence must be iterated, or otherwise referred to as a whole, make
a compound statement.
\bitem If a task can be carried out by a sequence of similar processes, where the
length of the sequence can be predetermined, decompose it into an iteration
of a command which represents the typical process of the sequence, using
the iteration variable or expressions containing it to represent those
aspects of the processes which differ.
For example, to print the numbers $$0, .01, .03, .06, .10, .15, \ldotss, .78, .91$$
we first find
a general formula for the $i↑{th}$ number.
The principle which generates the above sequence is that the numbers
differ by successive steps of $$.01 .02, .03, .04, \ldotss, .13$$
There are $i-1$ graduated steps
from the first number to the $i↑{th}$; the first step is $.01$, the last is
$.01(i-1)$, so the average step is $.005\times i$, and the $i↑{th}$ number is
$.005\times i \times (i-1)$. We can now program the printing as
\startcode
FOR I:=1 TO 14 DO
WRITE(.005*I*(I-1))
\endcode
(The later section on program variables and assignment provides methods
of design, for iterative programs, which would allow programming of this
problem even if we were unable to find the general formula for the $i↑{th}$
number, using instead the principle which generates the sequence.)
The decomposition method of program design is a powerful intellectual
tool, but it is not an automatic problem solver. It will not salvage a
fundamentally wrong approach to a problem, nor will it of itself create insight
into the possible methods of attacking a problem. Experienced programmers
draw on a large number of frequently useful methods; reading many programs is
the only way to acquire this experience. Instructors often provide a file of
example programs for reading and experiment.
\vfill
\exercise
Write a program to print a larger version of the diamond-shaped
pattern shown below:
\topoutput
*
***
*****
*******
*********
*******
*****
***
*
\botoutput
Your diamond, however, should be 19 symbols high, and 19 wide.
\endexercise
\exercise
As above, but with two diamonds side by side:
\topoutput
* *
*** ***
***** *****
**************
***** *****
*** ***
* *
\botoutput
\endexercise
{\bf Solutions}, omitting declarations, etc.:
\startcode
(1) FOR I:=1 TO 10 DO
BEGIN (* LINE OF 10-I SPACES, 2*I-1 STARS. *)
FOR J:=1 TO 10-I DO
WRITE(' ');
FOR K:=1 TO 2*I-1 DO
WRITE('*');
WRITELN
END;
FOR I:= 9 DOWNTO 1 DO
BEGIN (* LINES AS ABOVE, IN REVERSE ORDER *)
FOR J:=1 TO 10-I DO
WRITE(' ');
FOR K:=1 TO 2*I-1 DO
WRITE('*');
WRITELN
END
\endcode
\noindent
or
\startcode
FOR I:=-9 TO 9 DO
BEGIN
FOR J:=1 TO ABS(I) DO
WRITE(' ');
FOR K:=1 TO 19-2*ABS(I) DO
WRITE('*');
WRITELN
END
\endcode
\startcode
(2) FOR I:=-9 TO 9 DO
BEGIN
FOR L:=1 TO 2 DO
BEGIN
FOR J:=1 TO ABS(I) DO
WRITE(' ');
FOR K:=1 TO 19-2*ABS(I) DO
WRITE('*');
FOR J:=1 TO ABS(I) DO
WRITE(' ')
END;
WRITELN
END
\endcode
\specialbegin{Comments in Programs}
\sendindex{Comments in Programs}
A computer program embodies the intent of its author, but, if it is not
accompanied by an explanation, it may be unclear to anyone else. It may even
be unintelligible to its author himself after some lapse of time. It is
permitted in Pascal to insert comments anywhere in a program.
Comments are identified as such by being enclosed in starred parentheses
(* comment goes here *). When decomposing a problem, the early stages
of a decomposition can and should be kept as comments. A comment at the head
of a program should identify the program by author, date, and function, and
give the name of the file on which the program is kept. For example, the log
table program might have been written
\startcode
PROGRAM LOGTABLE;
(* PRINT A TABLE OF LOGARIITHMS FILE LOGTBL.PGO *)
(* ROBERT W. FLOYD, SEPT. 5, 1980 *)
VAR I,A,C : INTEGER;
BEGIN
(* PRINT THE TITLE OF THE TABLE. *)
FOR I := 1 TO 40 DO WRITE(' ');
WRITE('TABLE OF LOGARITHMS');
WRITELN;
WRITELN;
(* PRINT THE TABLE'S HEADING. *)
WRITE(' ');
FOR I:=0 TO 4 DO WRITE(I,' ');
WRITELN;
WRITELN;
(* PRINT THE BODY OF THE TABLE. *)
FOR A:=2 TO 19 DO
BEGIN
WRITE(5*A);
FOR C:=5*A TO 5*A+4 DO
WRITE(LN(C));
WRITELN
END
END.
\endcode
\specialbegin{Professionalism}
It is part of a professional attitude toward computer programming
to consider the future lifespan of every program you write. While the
exercises you now write are ephemeral, %check to see if right word
most computer programs are kept for a long time and used repeatedly.
The programmer should assume that:
\bitem The program contains as yet undetected errors,\sendindex{errors} which someone else may
have to locate and correct.
\bitem The problem which the program solves may later be redefined, requiring
modifica\-tions in the program.
\bitem The computer on which the program runs, or the other programs with
which it interacts, may be changed, requiring modifications in the program.
\bitem Some users of the program will not know exactly how it works.
%}
\noindent
A program should be designed to continue to be useful under all these adverse
circumstances. For this reason, a program should contain sufficient comments
to allow an experienced programmer to read it, in whole or in part, and
understand how it works. A comment at the beginning should tell who wrote
the program, what problem it solves, when it was last modified, and
what file it resides in. It should include enough information in its output
files to identify the problem solved, and (usually) to reproduce the input
data. It should provide through the terminal any necessary instruction to
an inexperienced user.